package com.itheima.tlias.mathcaptchaproducer;

import com.google.code.kaptcha.text.TextProducer;

import java.util.Random;

public class MathCaptchaProducer implements TextProducer {

    private final Random random = new Random();

    @Override
    public String getText() {
        // 随机生成两个数和一个运算符
        int num1 = random.nextInt(10) + 1; // 1~10
        int num2 = random.nextInt(10) + 1;
        int opIndex = random.nextInt(4); // 0:加, 1:减, 2:乘,  3:除
        char[] ops = {'+', '-', '*', '/'};
        char op = ops[opIndex];

        int result = 0;
        switch (op) {
            case '+' -> result = num1 + num2;
            case '-' -> result = num1 - num2;
            case '*' -> result = num1 * num2;
            case '/' -> result = num1 / num2;
        }

        // 返回格式如：5+3=8
        return num1 + " " + op + " " + num2 + " = " + result;
    }
}